package com.donleo.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.donleo.user.common.CommonResult;
import com.donleo.user.model.User;
import com.donleo.user.model.Word;
import com.donleo.user.service.UserService;
import com.donleo.user.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @author liangd
 * @since 2021-01-11 17:15
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private WordService wordService;

    @Value("${service.url.word}")
    private String wordAppUrl;

    @Value("${test.str}")
    private String testStr;

    /**
     * 初始化,项目启动的时候执行该方法
     */
    @PostConstruct
    public void init() {
//        List<User> users = userService.list();
//        System.out.println(users.get(0));
//        System.out.println(users.get(1));
//        System.out.println(users.get(2));
//        System.out.println(testStr);

    }

    /**
     * 查询所有用户
     */
    @GetMapping("/findAll")
    public CommonResult findAll() {
        List<User> list = userService.list(null);
        return CommonResult.success(list);
    }

    /****************************** RestTemplate调用单词模块服务测试 **********************************/

    /**
     * 根据用户编号获取单词服务的数据
     */
    @GetMapping("/findAllWord")
    public CommonResult findAllWord(@RequestParam String userCode) {
        return restTemplate.getForObject(wordAppUrl + "/word/findWordByUserCode?userCode={1}", CommonResult.class, userCode);
    }

    /**
     * 单个查询
     * getForEntity 测试
     */
    @GetMapping("/findWordById1/{id}")
    public CommonResult findWordById1(@PathVariable Integer id) {
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(wordAppUrl + "/word/findById1/{1}", CommonResult.class, id);
        System.out.println(forEntity);
        return forEntity.getBody();
    }

    /**
     * getForObject测试
     */
    @GetMapping("/findWordById2")
    public CommonResult findWordById2(@RequestParam Integer id) {
        return restTemplate.getForObject(wordAppUrl + "/word/findById2?id={1}", CommonResult.class, id);
    }

    /**
     * 根据Id集合查询
     * postForObject 测试
     */
    @GetMapping("/findWordByIds")
    public CommonResult findWordByIds() {
        Integer[] ids = {1,2,3};
        return restTemplate.postForObject(wordAppUrl + "/word/findByIds",ids, CommonResult.class);
    }

    /**
     * postForObject 测试
     */
    @GetMapping("/createWord")
    public CommonResult createWord() {
        Word word = Word.builder().id(101).chinese("自己").english("self").author("liangd").commitTime(new Date()).build();
        return restTemplate.postForObject(wordAppUrl + "/word/create", JSONObject.toJSON(word), CommonResult.class);
    }

    /**
     * put 测试
     */
    @GetMapping("/updateWord")
    public CommonResult updateWord() {
        Word word = Word.builder().id(101).chinese("爱好").english("hobby").author("liangd").commitTime(new Date()).build();
        restTemplate.put(wordAppUrl + "/word/update", JSONObject.toJSON(word),CommonResult.class);
        return CommonResult.success(null);
    }

    /**
     * delete 测试
     */
    @GetMapping("/deleteWord")
    public CommonResult deleteWord() {
        Integer id = 101;
        restTemplate.delete(wordAppUrl + "/word/delete?id={1}", id);
        return CommonResult.success(null);
    }

    /***************************OpenFeign声明式调用单词模块服务***************************/

    /**
     * 查询所有
     */
    @GetMapping("/feignFindAllWord")
    public CommonResult feignFindAllWord(){
        System.out.println(testStr);
        return wordService.feignFindAllWord();
    }

    /**
     * 单个
     */
    @GetMapping("/feignFindWordById")
    public CommonResult feignFindWordById(@RequestParam Integer id){
        return wordService.feignFindWordById(id);
    }

    /**
     * 新增
     */
    @GetMapping("/feignCreateWord")
    public CommonResult feignCreateWord(){
        Word word = Word.builder().id(101).chinese("自己").english("self").author("liangd").commitTime(new Date()).build();
        return wordService.feignCreateWord(word);
    }

    /**
     * 修改
     */
    @GetMapping("/feignUpdateWord")
    public CommonResult feignUpdateWord(){
        Word word = Word.builder().id(101).chinese("爱好").english("hobby").author("liangd").commitTime(new Date()).build();
        return wordService.feignUpdateWord(word);
    }

    /**
     * 删除
     */
    @GetMapping("/feignDeleteWordById")
    public CommonResult feignDeleteWordById(){
        Integer id = 101;
        return wordService.feignDeleteWordById(id);
    }

}
